package com.kakavr.shop.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.base.service.BaseService;
import com.kakavr.page.Pagination;
import com.kakavr.page.constant.PaginationConstant;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.dao.IShopAdminDao;
@Service
public class ShopAdminService extends BaseService implements IShopAdminService {
	@Resource
	private IShopAdminDao shopAdminDao;

	@Override
	public void addShopAndEmp(Map<String, Object> map) {
		if(shopAdminDao.getShopCodeCount(map) < 1){
			map.put("createTime", DateTime.now().toDate());
			shopAdminDao.insertShop(map);
			map.put("storeId", map.get("id"));
			this.addEmployee(map);
		}else{
			throw new ValidateParamException(MsgConstants.MSG_CODE_1003,
					MsgConstants.MSG_CONTENT_1003, "店铺登录名");
		}
	}

	@Override
	public void addEmployee(Map<String, Object> map) {
		if(shopAdminDao.getEmpUserName(map) < 1){
			shopAdminDao.insertEmployee(map);
		}else{
			throw new ValidateParamException(MsgConstants.MSG_CODE_1003,
					MsgConstants.MSG_CONTENT_1003, "用户名");
		}
	}

	@Override
	public void updateShopAndEmp(Map<String, Object> map) {
		this.updateShop(map);
		this.updateEmployee(map);
	}

	@Override
	public void updateEmployee(Map<String, Object> map) {
		shopAdminDao.updateEmployee(map);
	}

	@Override
	public Pagination<Map<String, Object>> selectShopByPage(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String, Object>>(
				shopAdminDao.selectShopList(map));
	}

	@Override
	public Map<String, Object> getShopInfo(Map<String, Object> map) {
		return shopAdminDao.selectShopInfo(map);
	}

	@Override
	public void updateShop(Map<String, Object> map) {
		shopAdminDao.updateShop(map);
	}

	@Override
	public List<Map<String, Object>> selectShopList(Map<String, Object> map) {
		return shopAdminDao.selectShopList(map);
	}

	@Override
	public void updateShopTimesTrigger() {
		shopAdminDao.updateShopTimesTrigger();
	}
	
	

}
